package com.wrl.mmall.controller.backend;

import com.wrl.mmall.common.ServerResponse;
import com.wrl.mmall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 后端 商品分类管理
 * Created by Wang Renlei on 2018/10/20.
 */
@Slf4j
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 添加商品分类
     *
     * @param categoryName 分类名称
     * @param parentId     默认为0
     * @return
     */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(String categoryName, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        log.info("CategoryManageController.addCategory");
        if (StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("必要参数为空");
        }
        return iCategoryService.addCategory(categoryName, parentId);
    }

    /**
     * 修改分类名字
     *
     * @param categoryName 分类名称
     * @param categoryId   分类id
     * @return
     */
    @RequestMapping("update_category_name.do")
    @ResponseBody
    public ServerResponse updateCategoryName(String categoryName, Integer categoryId) {
        log.info("CategoryManageController.updateCategoryName");
        if (StringUtils.isBlank(categoryName) || categoryId == null) {
            return ServerResponse.createByErrorMessage("必要参数为空");
        }
        return iCategoryService.updateCategoryName(categoryName, categoryId);
    }

    /**
     * 寻找这个爸爸的所有儿子详情（如果输入二级分类的id，则查看的是它之下的三级分类）
     *
     * @param categoryId 默认为0（即查看所有一级分类）
     * @return
     */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        log.info("CategoryManageController.getChildrenParallelCategory");
        //查询category子节点的信息，并且保持平级
        return iCategoryService.getChildrenParallelCategory(categoryId);
    }

    /**
     * 寻找这个爸爸的所有儿子，儿子的儿子，儿子的儿子的儿子......(只获取id)
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildren(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        log.info("CategoryManageController.getCategoryAndDeepChildren");
        //查询当前节点的id和递归子节点id
        return iCategoryService.selectCategoryAndChildrenById(categoryId);
    }
}
